#include"common.h"
using namespace std;
extern BuiltinCmd blt[MAX_BUILTIN_FUNC_NUM];
extern char buf[80];
int getworkdirectory(){
	FILE *fp=fopen("D:\\self-git\\git\\directory\\directory.txt","r");
	char dir[MAX_BUILTIN_DESC_LENGTH];
	char ch=0;
    fseek(fp,-1,SEEK_END);
    fread(&ch,1,1,fp); 
    while (ch!='\n'){
      fseek(fp,-2,SEEK_CUR);
      fread(&ch,1,1,fp);
    }
    fgets(dir,MAX_BUILTIN_DESC_LENGTH,fp);
    chdir(dir);
   // getcwd(buf, sizeof(buf));
   // printf("current working directory : %s\n", buf);
    fclose(fp);
}
int Cmdpaser(int retc,char **retv){
//	for (int i=1;i<retc;++i)printf("%s\n",retv[i]);
//cout<<retv[1]<<endl;
	if (strcmp("git",retv[1])){printf("command not found\n");return 0;}
	int findpos=findBuiltinCmd(retv[2]);
	if (findpos==-1){printf("command not found\n");return 0;}
	   	if (strcmp("init",blt[findpos].name)==0)git_init_cmdpline(findpos,retc,retv);	
		if (strcmp("pwd",blt[findpos].name)==0)git_pwd_cmdpline();
		if (strcmp("diff",blt[findpos].name)==0)git_diff_cmdpline(findpos,retc,retv); 
		if (strcmp("commit",blt[findpos].name)==0)git_commit_cmdpline(findpos,retc,retv);
		if (strcmp("rm",blt[findpos].name)==0)git_rm_cmdpline();
		if (strcmp("log",blt[findpos].name)==0)git_log_cmdpline();
		if (strcmp("reset",blt[findpos].name)==0)git_reset_cmdpline();
}
int Cmdrunning(int retc,char **retv){
	if (strcmp("git",retv[1]))return 0;
	int findpos=findBuiltinCmd(retv[2]);
	if (findpos==-1)return 0;
	if (strcmp("diff",blt[findpos].name)==0) git_diff_run(retc,retv);
	if (strcmp("pwd",blt[findpos].name)==0) git_pwd_run();
	if (strcmp("init",blt[findpos].name)==0) git_init_run(retc,retv);
    if (strcmp("add",blt[findpos].name)==0) git_add_run(retc,retv);
    if (strcmp("commit",blt[findpos].name)==0)git_commit_run(retc,retv);
    if (strcmp("rm",blt[findpos].name)==0)git_rm_run(retc,retv);
    if (strcmp("log",blt[findpos].name)==0)git_log_run();
    if (strcmp("reset",blt[findpos].name)==0)git_reset_run(retc,retv);
}
int main(int argc,char **argv){
	getworkdirectory();
	initBuiltin();
	Cmdpaser(argc,argv);
	Cmdrunning(argc,argv);
}
